/*
 * CachedThermistorModel.hpp
 *
 * Copyright 2012 Pieter Agten
 *
 * This file is part of tltgen.
 *
 * tltgen is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * tltgen is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with tltgen.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * @file   CachedThermistorModel.hpp
 * @author Pieter Agten (pieter.agten@gmail.com)
 * @date   4 sep 2012
 */

#ifndef CACHEDTHERMISTORMODEL_HPP
#define CACHEDTHERMISTORMODEL_HPP

#include <boost/shared_ptr.hpp>
#include <boost/weak_ptr.hpp>
#include <boost/signals2.hpp>

#include "ThermistorTable.hpp"

#include "ThermistorModel.hpp"

using boost::shared_ptr;
using boost::weak_ptr;

namespace tltgen {

  class CachedThermistorModel : public ThermistorModel  {
  public:
    CachedThermistorModel(const shared_ptr<ThermistorModel>& thermistorModel, size_t cacheSize = 10);
    virtual ~CachedThermistorModel();

    static bool isValidThermistorModel(const shared_ptr<ThermistorModel>& thermistorModel);
    static bool isValidCacheSize(size_t cacheSize);

    virtual size_t getCacheSize() const;

    virtual shared_ptr<Thermistor> generate(const shared_ptr<ThermistorTable>& thermistorTable, const LeastSquaresSolver& solver);
    virtual void invalidate(ThermistorTable* thermistorTable);
    virtual void clearCache();

    virtual std::string getName() const
    {
      return this->thermistorModel->getName();
    }
    virtual unsigned int getMinNbValues() const
    {
      return this->thermistorModel->getMinNbValues();
    }

    virtual unsigned int getNbParameters() const
    {
      return this->thermistorModel->getNbParameters();
    }
    virtual std::string getParameterName(unsigned int pi) const
    {
      return this->thermistorModel->getParameterName(pi);
    }
    virtual ParameterType getParameterType(unsigned int pi) const
    {
      return this->thermistorModel->getParameterType(pi);
    }

    virtual void getInitialGuess(double *guess) const
    {
      return this->thermistorModel->getInitialGuess(guess);
    }

    virtual bool isParameterFixed(unsigned int pi) const
    {
      return this->thermistorModel->isParameterFixed(pi);
    }

    virtual bool hasParameterLowerBound(unsigned int pi) const
    {
      return this->thermistorModel->hasParameterLowerBound(pi);
    }
    virtual double getParameterLowerBound(unsigned int pi) const
    {
      return this->thermistorModel->getParameterLowerBound(pi);
    }
    virtual bool hasParameterUpperBound(unsigned int pi) const
    {
      return this->thermistorModel->hasParameterUpperBound(pi);
    }
    virtual double getParameterUpperBound(unsigned int pi) const
    {
      return this->thermistorModel->getParameterUpperBound(pi);
    }

    virtual void applyModelFunction(const ThermistorTable* thermistorTable, const double *params, double *residues) const
    {
      return this->thermistorModel->applyModelFunction(thermistorTable, params, residues);
    }
    virtual void applyModelDerivative(const ThermistorTable* thermistorTable, const double *params, double *jacobian) const
    {
      return this->thermistorModel->applyModelDerivative(thermistorTable, params, jacobian);
    }

  protected:
    virtual void onThermistorTableChanged(ThermistorTable* thermistorTable, ThermistorTable::Event e, ThermistorTableRow *tableRow);

    const shared_ptr<ThermistorModel> thermistorModel;
    const size_t cacheSize;
    
    std::list<boost::tuple<weak_ptr<ThermistorTable>, shared_ptr<Thermistor>, boost::signals2::connection> > thermistors;
  };

}

#endif
